package com.chuangyang.fixboxmaster.ui.fragment.orderprice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;
import com.chuangyang.fixboxmaster.bean.CostItemInfo;
import com.chuangyang.fixboxmaster.bean.CostItemInfos;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.CompleteApplianceInfoActivity;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.adapter.RepairTypeListAdapter;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/7/30.
 */
public class RpeairTypeFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private View mContentView;
    private ListView mTypeListView;
    private RepairTypeListAdapter mAdapter;
    private ArrayList<CostItem> mTypeList = new ArrayList<>();
    private CostItem mSelectItem;
    private String mTransactionId;
    private String mSpecificationId;
    private String id;

    public static RpeairTypeFragment newInstance(){
        return new RpeairTypeFragment();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_repair_type,container,false);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            mTransactionId = getArguments().getString(ModuleActivity.TRANSCATION_ID);
            mSpecificationId = getArguments().getString(ModuleActivity.SPECIFICATION_ID);
            mSelectItem = (CostItem)bundle.getSerializable(ModuleActivity.REPAIR_TYPE);
            id=getArguments().getString("id");
        }
        initView();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initView(){
        mTypeListView = (ListView)mContentView.findViewById(R.id.repair_type_list);
        mTypeListView.setOnItemClickListener(this);
        mAdapter = new RepairTypeListAdapter(mTypeList,mSelectItem);
        mTypeListView.setAdapter(mAdapter);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadDiagnosisFeeData();
    }

    private void loadDiagnosisFeeData(){
        HashMap<String, String> param = new HashMap<>();
        param.put("areaId", ""+1);
        param.put("transactionId", mTransactionId);
        param.put("specificationId", mSpecificationId);
        param.put("id",id);
        addRequest(new GsonRequest<>(FixBoxApi.GET_DIAGNOSIS_FREE, param, CostItemInfos.class, responseDiagnosisFeeListener));
    }


    private Response.Listener<CostItemInfos> responseDiagnosisFeeListener = new Response.Listener<CostItemInfos>() {
        @Override
        public void onResponse(CostItemInfos response) {

            if (response.result != null)
            {
                mTypeList.clear();
                mTypeList.addAll(Arrays.asList(response.result));
                mAdapter.notifyDataSetChanged();
            }
            showResult();
        }
    };


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CostItem costItem = mTypeList.get(position);
        mAdapter.setSelectItem(costItem);
        mAdapter.notifyDataSetChanged();
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("cost_item",costItem);
        intent.putExtras(bundle);
        getActivity().setResult(Activity.RESULT_OK,intent);
        getActivity().finish();
    }
}

